//
//  RNOLQuickRegisterView.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/26.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLQuickRegisterView.h"

@interface RNOLQuickRegisterView()<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UILabel *phoneMarkView;
@property (weak, nonatomic) IBOutlet UIButton *checkboxButton;
@property (weak, nonatomic) IBOutlet UILabel *protocolLabel;

@property (weak, nonatomic) IBOutlet UILabel *phoneTipLabel;
@property (weak, nonatomic) IBOutlet UILabel *verifyCodeTipLabel;
@property (weak, nonatomic) IBOutlet UILabel *passwordTipLabel;

@end


@implementation RNOLQuickRegisterView

- (void)awakeFromNib{
    [super awakeFromNib];
    [self.protocolLabel moreColorWithAttributeString:@"《融牛在线用户协议》" color:kAppMainColor];
    self.protocolLabel.userInteractionEnabled = YES;
    [self.protocolLabel addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        //查看协议
        NSLog(@"查看协议");
    }];

    //设置左边圆角
    [self.phoneMarkView setCornerOnLeft:5];
    [self.registerButton setBackgroundColor: kAppMainColor forState:UIControlStateNormal];
    [self.registerButton setBackgroundColor: kTextLightGrayColor forState:UIControlStateDisabled];

    //MARK:- 监听键盘弹出
    [[kNotificationCenter rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        dispatch_main_async_safe(^{
            [self verifyLogic];
        });
    }];
    
    //MARK:- 监听键盘消失
    [[kNotificationCenter rac_addObserverForName: UIKeyboardWillHideNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        dispatch_main_async_safe(^{
            [self verifyLogic];
        });
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.phoneTextField becomeFirstResponder]; //默认先弹出键盘
    });
    
    [self.phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.codeTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

}

- (IBAction)checkProtocolAction:(UIButton *)sender {
    sender.selected = !sender.selected;
}


//显示或隐藏密码
- (IBAction)displayPasswordAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.passwordTextField.secureTextEntry = !self.passwordTextField.secureTextEntry;
}

//注册
- (IBAction)registerAction:(UIButton *)sender {
    !self.registerBlock? : self.registerBlock();
}

//获取验证码
- (IBAction)getVerityCodeAction:(UIButton *)sender {
    if (self.phoneTextField.text.length != 11) {
        [kRootVC showErrorMessageWithText:@"请先输入手机号,再获取验证码"];
        return ;
    }
    !self.getVerifityCodeBlock? : self.getVerifityCodeBlock();
    [sender addStartTime:60 title:@"重新获取" waitTittle:@"秒"];
}


//跳回页面
- (IBAction)skipToLoginPageAction:(UIButton *)sender {
    [self.viewController.navigationController popViewControllerAnimated:YES];
}


#pragma mark- <UITextFieldDelegate>
// became first responder
- (void)textFieldDidBeginEditing:(UITextField *)textField{

}

// 失去第一相应者
- (void)textFieldDidEndEditing:(UITextField *)textField{

    
}

// 限制输入
- (void)textFieldDidChange:(UITextField *)textField{
    if (self.phoneTextField == textField) {
         if (textField.text.length > 11) {
             textField.text = [textField.text substringToIndex:11];
         }
    }
    
    if (self.codeTextField == textField) {
        if (textField.text.length > 6) {
            textField.text = [textField.text substringToIndex:6];
        }
    }
    
    if (self.passwordTextField == textField) {
        if (textField.text.length > 20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
}

#pragma mark -  校验逻辑
- (void)verifyLogic{
    NSString *message = @"";
    NSString *phoneNumber  = self.phoneTextField.text;
    NSString *verifyCode = self.codeTextField.text;
    NSString *password = self.passwordTextField.text;
    
    if (!self.phoneTextField.isFirstResponder) {
        if (phoneNumber.length != 11) {
            message = phoneNumber.length? @"你的手机号不合法" : @"手机号不可为空";
            [self alertTipStyle: self.phoneTipLabel text: message];
        }else{
            [self covertBackNormalStyle: self.phoneTipLabel text: @"手机号"];
        }
    }
//    else{
//        [self covertBackNormalStyle: self.phoneTipLabel text: @"手机号"];
//    }

    if (!self.codeTextField.isFirstResponder && phoneNumber.length == 11) {
        if (verifyCode.length != 6 ) {
            message = @"验证码不合法" ;
            [self alertTipStyle: self.verifyCodeTipLabel text: message];
        }else{
            [self covertBackNormalStyle:self.verifyCodeTipLabel text: @"验证码"];
        }
    }
//    else{
//        [self covertBackNormalStyle:self.verifyCodeTipLabel text: @"验证码"];
//    }

    if (!self.passwordTextField.isFirstResponder && phoneNumber.length == 11 && verifyCode.length == 6) {
        if (password.length < 6 || password.length > 20) {
            message = @"密码格式有误,请确认后输入";
            [self alertTipStyle: self.passwordTipLabel text: message];
        }else{
            [self covertBackNormalStyle: self.passwordTipLabel text: @"密码"];
        }
    }
//    else{
//        [self covertBackNormalStyle: self.passwordTipLabel text: @"密码"];
//    }
}

#pragma mark -提示
- (void)alertTipStyle:(UILabel *)label text:(NSString *)text{
    label.textColor = kRedBagColor;
    label.text = text;
}

#pragma mark- 恢复
- (void)covertBackNormalStyle:(UILabel *)label text:(NSString *)text{
    label.textColor = kTextLightColor;
    label.text = text;
}


@end
